package com.ntanh.mealplanner.main.dao;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.ntanh.mealplanner.base.model.BaseEntity;
import com.ntanh.mealplanner.main.objects.DayMeal;
import com.ntanh.mealplanner.main.objects.WeekMeal;

import java.text.ParseException;
import java.util.Date;

/**
 * Created by Anh Nguyen on 2/27/2015.
 */
public class WeekMealDao extends BaseDao {

    public WeekMealDao(Context context) {
        super(context);
    }

    public WeekMeal getById(String author, Integer id) {
        DayMealDao dayMealDao = new DayMealDao(mContext);
        WeekMeal weekMeal = null;
        String query = "SELECT " + "*"
                + " FROM " + DataBaseHelper.WEEK_MEAL_TABLE
                + " WHERE id = " + id;

        Log.d("query", query);
        Cursor cursor = database.rawQuery(query, null);
        while (cursor.moveToNext()) {
            weekMeal = convertFromCursor(cursor, author, dayMealDao, false);
        }
        return weekMeal;
    }

    public WeekMeal getByName(String author, String name) {
        DayMealDao dayMealDao = new DayMealDao(mContext);
        WeekMeal weekMeal = null;
        String query = "SELECT " + "*"
                + " FROM " + DataBaseHelper.WEEK_MEAL_TABLE
                + " WHERE " + WeekMeal.NAME + " = '" + name + "'";
        Log.d("query", query);
        Cursor cursor = database.rawQuery(query, null);
        while (cursor.moveToNext()) {
            weekMeal = convertFromCursor(cursor, author, dayMealDao, false);
        }
        return weekMeal;
    }

    public WeekMeal getByStartDate(String author, Date startDate) {
        DayMealDao dayMealDao = new DayMealDao(mContext);
        WeekMeal weekMeal = null;
        String query = "SELECT " + "*"
                + " FROM " + DataBaseHelper.WEEK_MEAL_TABLE
                + " WHERE " + WeekMeal.START_DATE + " = '" + BaseEntity.dateFormatter.format(startDate) + "'";

        Log.d("query", query);
        Cursor cursor = database.rawQuery(query, null);
        while (cursor.moveToNext()) {
            weekMeal = convertFromCursor(cursor, author, dayMealDao, false);
        }
        return weekMeal;
    }

    private WeekMeal convertFromCursor(Cursor cursor, String author, DayMealDao dayMealDao, boolean lazy) {
        WeekMeal weekMeal = new WeekMeal(author);
        weekMeal.setId(cursor.getInt(0));
        try {
            weekMeal.setCreatedDate(BaseEntity.dateFormatter.parse(cursor.getString(1)));
        } catch (ParseException e) {
            weekMeal.setCreatedDate(null);
        }
        try {
            weekMeal.setModifiedDate(BaseEntity.dateFormatter.parse(cursor.getString(2)));
        } catch (ParseException e) {
            weekMeal.setModifiedDate(null);
        }
        weekMeal.setCreatedBy(cursor.getString(3));
        weekMeal.setModifiedBy(cursor.getString(4));
        weekMeal.setActive(cursor.getInt(5) == 1);
        weekMeal.setDeleted(cursor.getInt(6) == 1);
        weekMeal.setSynced(cursor.getInt(7) == 1);
        if (!lazy) {
            String mealStr = cursor.getString(8);
            if(mealStr != null) {
                if (mealStr.contains(BaseEntity.entitySplitSign)) {
                    String[] array = mealStr.split(BaseEntity.entitySplitSign);
                    for (String dayMealId : array) {
                        DayMeal dayMeal = dayMealDao.getById(author, Integer.parseInt(dayMealId));
                        if (dayMeal != null) {
                            weekMeal.getDayMealList().add(dayMeal);
                        }
                    }
                } else if (!mealStr.isEmpty()) {
                    DayMeal dayMeal = dayMealDao.getById(author, Integer.parseInt(mealStr));
                    if (dayMeal != null) {
                        weekMeal.getDayMealList().add(dayMeal);
                    }
                }
            }
        }

        try {
            weekMeal.setStartDate(BaseEntity.dateFormatter.parse(cursor.getString(9)));
        } catch (ParseException e) {
            weekMeal.setStartDate(null);
        }

        return weekMeal;
    }
}
